'use strict';

angular.module('myApp.decisions', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/decisions', {
    templateUrl: 'views/decisions/decisions.html',
    controller: 'DecisionsCtrl'
  });
}])

.controller('DecisionsCtrl', function($scope,$rootScope, $http) {

$scope.addDecision = function(name) {
	var body = JSON.stringify({
  	'decision': {
    	'name': name,
    	'group_id': '1',
    	'project_id': '2'
  	}});
  		$http.post('http://localhost:3000/api/v1/decisions?remember_token='+$rootScope.token,body)
  		.success(function(data, status, headers, config) {
    		console.log(status + ' ' + JSON.stringify(data));
    	})
  		.error(function(data, status, headers, config) {
  			console.log(status + ' ' + JSON.stringify(data));
    	});
	};

	if ($rootScope.token != undefined) {
		var body = {}
		$http.get('http://localhost:3000/api/v1/decisions?remember_token='+$rootScope.token,body)
  		.success(function(data, status, headers, config) {
    		console.log(status + ' ' + JSON.stringify(data));
    		$scope.decisions = data;
    	})
  		.error(function(data, status, headers, config) {
  			console.log(status + ' ' + JSON.stringify(data));
    	});
	}
});